// 暴力做法 直接就是遍历所有的组合 进行if判断来进行结果更新
//优化做法：就是类似固定右端点 记录便路过的左端点的值就实现了一次遍历数组
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    int n;
    vector<string> strs;
    string str1, str2;
    int count[2] = {-1, -1}; // 0下标记录的是str1的最近位置 1下标是str2的最近位置
    cin >> n >> str1 >> str2;
    strs.resize(n);
    for (int i = 0; i < n; i++)
    {
        cin >> strs[i];
    }
    int ret = 1e6;
    for (int i = 0; i < n; i++)
    {
        if (strs[i] == str1)
            count[0] = i;
        if (strs[i] == str2)
            count[1] = i;
        if (strs[i] == str1)
        {
            count[0] = i;
            if (count[1] != -1)
            {
                ret = min(ret, i - count[1]);
            }
        }
        else if (strs[i] == str2)
        {
            count[1] = i;
            if (count[0] != -1)
            {
                ret = min(ret, i - count[0]);
            }
        }
    }
    if (count[0] == -1 || count[1] == -1)
        ret = -1;
    cout << ret;
    return 0;
}